package com.bee.littlebeetravel.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.bee.littlebeetravel.R;
import com.bee.littlebeetravel.fragment.ClubFragment;
import com.bee.littlebeetravel.fragment.DiscoverFragment;
import com.bee.littlebeetravel.fragment.FriendFragment;
import com.bee.littlebeetravel.fragment.HomeFragment;
import com.bee.littlebeetravel.fragment.MyFragment;
import com.bee.littlebeetravel.utils.AppManager;
import com.bee.littlebeetravel.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {
    // 退出时间
    private long mExitTime;
    @BindView(R.id.rg_group)
    RadioGroup rg_group;
    public Fragment[] mFragments;
    // 点击下标
    private int mIndex;

    // 点击事件
    @OnClick({R.id.rb_home, R.id.rb_club, R.id.rb_discover, R.id.rb_friend, R.id.rb_my})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_home:
                setIndexSelected(0);
                break;
            case R.id.rb_club:
                setIndexSelected(1);
                break;
            case R.id.rb_discover:
                setIndexSelected(2);
                break;
            case R.id.rb_friend:
                setIndexSelected(3);
                break;
            case R.id.rb_my:
                setIndexSelected(4);
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AppManager.getInstance().addActivity(this);
        ButterKnife.bind(this);
        rg_group.check(R.id.rb_home);
        initFragments();
    }

    // 初始化fragment
    private void initFragments() {
        HomeFragment homeFragment = new HomeFragment();
        ClubFragment clubFragment = new ClubFragment();
        DiscoverFragment discoverFragment = new DiscoverFragment();
        FriendFragment friendFragment = new FriendFragment();
        MyFragment myFragment = new MyFragment();
        mFragments = new Fragment[]{homeFragment, clubFragment, discoverFragment, friendFragment, myFragment};
        FragmentTransaction ft =
                getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fl_content, homeFragment).commit();
        setIndexSelected(0);
    }

    // 设置显示指定的fragment
    public void setIndexSelected(int index) {
        if (mIndex == index) {
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        //隐藏
        ft.hide(mFragments[mIndex]);
        //判断是否添加
        if (!mFragments[index].isAdded()) {
            ft.add(R.id.fl_content, mFragments[index]).show(mFragments[index]);
        } else {
            ft.show(mFragments[index]);
        }
        // 异步防止频繁操作崩溃
        fragmentManager.executePendingTransactions();
        ft.commit();
        //再次赋值
        mIndex = index;
    }

    // APP退出管理
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if ((System.currentTimeMillis() - mExitTime) > 2000) {
                    ToastUtil.showShort(getApplicationContext(), "再按一次退出程序");
                    mExitTime = System.currentTimeMillis();
                } else {
                    AppManager.getInstance().exitApp();
                    finish();
                }
                break;
            default:
                return super.onKeyDown(keyCode, event);
        }
        return true;
    }
}